<?php
namespace app\admin\controller;

use app\admin\logic\AfterSaleLogic;
use app\common\model\AfterSale as CommonAfterSale;

class AfterSaleController extends Controller
{
    /**
     * Desc: 列表
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $get = $this->request->get();
            $list=AfterSaleLogic::lists($get);
            return json([
                'code' =>1,
                'msg'  =>'',
                'data' =>$list
            ]);
        }
        $this->assign('status', CommonAfterSale::getStatusDesc(true));
        return $this->fetch();
    }

    /**
     * Desc: 详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        $detail = AfterSaleLogic::getDetail($id);
        $this->assign('detail', $detail);
        return $this->fetch();
    }


    /**
     * Desc: 同意
     */
    public function agree()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\AfterSale.agree');
            if (true !== $check) {
                return error($check);
            }
            AfterSaleLogic::agree($post['id'],$this->user->id);
            return success('操作成功');
        }
    }

    /**
     * Desc: 拒绝
     */
    public function refuse()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\AfterSale.refuse');
            if (true !== $check) {
                return error($check);
            }
            AfterSaleLogic::refuse($post,$this->user->id);
            return success('操作成功');
        }
    }

    /**
     * Desc: 收货
     */
    public function take()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\AfterSale.take');
            if (true !== $check) {
                return error($check);
            }
            AfterSaleLogic::takeGoods($post,$this->user->id);
            return success('操作成功');
        }
    }

    /**
     * Desc: 拒绝收货
     */
    public function refuseGoods()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\AfterSale.refuse_goods');
            if (true !== $check) {
                return error($check);
            }
            AfterSaleLogic::refuseGoods($post,$this->user->id);
            return success('操作成功');
        }
    }


    /**
     * Desc: 确认退款
     */
    public function confirm()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post('');
            $check = $this->validate($post, 'app\admin\validate\AfterSale.confirm');
            if (true !== $check) {
                return error($check);
            }
            $res = AfterSaleLogic::confirm($post,$this->user->id);
            if ($res === true){
                return success('操作成功');
            }
            return error($res);
        }
    }
}